package com.atguigu.c_designed.m2stucturalModel.s6FlyWeight;

import java.util.HashMap;
import java.util.Map;

public class FlyWeightClient {

    public static void main(String[] args) {
        Circle red = FlayweightFactory.getCircle("red");
        red.setX(100);
        red.setY(100);
        red.setRadius(100);
        red.draw();
        Circle red1 = FlayweightFactory.getCircle("red");
        red1.setX(101);
        red1.setY(101);
        red1.setRadius(100);
        red1.draw();
        Circle green = FlayweightFactory.getCircle("green");
        green.setX(101);
        green.setY(101);
        green.setRadius(100);
        green.draw();
    }

}

interface Shape {
    public void draw();
}
class Circle implements Shape {

    private int x;
    private int y;
    private int radius;

    private String color;

    public Circle(String color) {
        this.color = color;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    @Override
    public void draw() {
        System.out.println("Circle: Draw() [Color = " + color
                +", x = " + x +", y =" + y +", radius = " + radius);
    }
}

class FlayweightFactory {

    static Map<String,Shape> circleMap = new HashMap<>();

    public static Circle getCircle(String color) {
        Circle circle = (Circle) circleMap.get(color);
        if (circle == null){
            circle = new Circle(color);
            circleMap.put(color,circle);
            System.out.println("Creating circle of color : " + color);
        }
        return circle;
    }
}